package com.itzb.asone.rss;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.itzb.asone.rss.model.RssChannelBean;
import com.itzb.asone.rss.model.RssItemBean;

public class RSSFeedParser {

	private static String RSS_DOM_ROOT_TITLE = "//channel/title";

	private static String RSS_DOM_ROOT_LINK = "//channel/link";

	private static String RSS_DOM_ROOT_DESCRIPTION = "//channel/description";
	
	private static String RSS_DOM_ROOT_GENERATOR = "//channel/generator";
	
	private static String RSS_DOM_ROOT_LASTBUILDDATE = "//channel/lastBuildDate";

	private static String RSS_DOM_CHILRDEN_ROOT = "//channel/item";

	private static String RSS_DOM_CHILRDEN_ROOT_TITLE = "title";

	private static String RSS_DOM_CHILRDEN_ROOT_LINK = "link";

	private static String RSS_DOM_CHILRDEN_ROOT_PUBDATE = "pubDate";

	private static String RSS_DOM_CHILRDEN_ROOT_DESCRIPTION = "description";
	
	private static String RSS_DOM_CHILRDEN_ROOT_CATEGORY = "category";
	
	private static String RSS_DOM_CHILRDEN_ROOT_AUTHOR = "author";

	private static Document document;

	private static Document parse(URL url) throws DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read(url);
		return document;
	}

	private static void parseUrl(URL url) throws DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read(url);
	}

	private static List getXmlInfo(String path, URL url) {
		List info = new ArrayList();
		try {
			Document document = parse(url);
			info = document.selectNodes(path);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return info;
	}

	private static List getXmlInfo(String path) throws DocumentException {
		List info = new ArrayList();
		info = document.selectNodes(path);
		return info;
	}

	private static Element getFirstNodeTitle(String path, URL url) {
		List list = getXmlInfo(path, url);
		Element element = (Element) list.get(0);
		return element;
	}

	private static Element getFirstNodeTitle(String path) throws DocumentException {
		List list = getXmlInfo(path);
		Element element = (Element) list.get(0);
		return element;
	}

	/**
	 * 通过rss地址获取rss内容
	 * 
	 * @param uri
	 * @return
	 */
	public static RssChannelBean getRssBean(String uri) {

		RssChannelBean rssChannelBean = new RssChannelBean();
		List<RssItemBean> rssItemBeanList = new ArrayList<RssItemBean>();

		URL url;
		try {
			url = new URL(uri);

			parseUrl(url);

			List listRoot = getXmlInfo("//channel");
			for (Iterator iter = listRoot.iterator(); iter.hasNext();) {
				Element element = (Element) iter.next();

				Node title = element.selectSingleNode(RSS_DOM_ROOT_TITLE);
				Node link = element.selectSingleNode(RSS_DOM_ROOT_LINK);
				Node description = element.selectSingleNode(RSS_DOM_ROOT_DESCRIPTION);
				Node generator = element.selectSingleNode(RSS_DOM_ROOT_GENERATOR);
				Node lastBuildDate = element.selectSingleNode(RSS_DOM_ROOT_LASTBUILDDATE);
				rssChannelBean.setTitle(title.getText());
				rssChannelBean.setDescription(description.getText());
				rssChannelBean.setLink(link.getText());
				rssChannelBean.setGenerator(generator.getText());
				rssChannelBean.setLastBuildDate(lastBuildDate.getText());

			}
			
			/**
			 * 获取内容项
			 */
			List list = getXmlInfo(RSS_DOM_CHILRDEN_ROOT);
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				RssItemBean rssItemBean = new RssItemBean();
				Element element = (Element) iter.next();
				Node title = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_TITLE);
				Node link = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_LINK);
				Node time = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_PUBDATE);
				Node category = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_CATEGORY);
				Node author = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_AUTHOR);
				Node description = element .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_DESCRIPTION);
				rssItemBean.setTitle(title.getText());
				rssItemBean.setPubDate(time.getText());
				rssItemBean.setDescription(description.getText());
				rssItemBean.setLink(link.getText());
				rssItemBean.setCategory(category.getText());
				rssItemBean.setAuthor(author.getText());
				rssItemBeanList.add(rssItemBean);

			}
			rssChannelBean.setRssItemBeanList(rssItemBeanList);
			return rssChannelBean;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	public static void main(String args[]) throws IOException {

		String url = "http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=960339491";
		RssChannelBean rs = RSSFeedParser.getRssBean(url);
		System.out.println(rs.toString());

	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public Document getDocument() {
		return document;
	}

}
